package 比屋作业.day16;
import java.util.*;
public class D16_2 {
    public static void main(String[] args) {
    Systerm system= new Systerm();
    system.addProduct(new Product("001","苹果"));
    system.addProduct(new Product("002","橘子"));
    system.displayProducts();
    for (Product product:system.findProduct("001")){
        System.out.println("查找商品成功！"+product);
    }
        System.out.println("删除商品苹果！");
        if (system.removeProduct("001","苹果")){
            System.out.println("删除成功！");
        }else {
        System.out.println("删除失败！");
        }
        System.out.println("删除后的商品列表：");
        system.displayProducts();
    }
}
class Product{
    private String ID;
    private String name;
    private int price;
    public Product(String ID,String name){
        this.ID=ID;
        this.name=name;
    }

    public String getName() {
        return name;
    }
    public String getID(){
        return ID;
    }

    @Override
    public String toString() {
        return "商品编号"+ID+"商品名称："+name;
    }

    @Override
    public boolean equals(Object obj) {
        if (this==obj)return true;
        if (obj==null||getClass()!=obj.getClass())return false;
        Product product=(Product) obj;
        return this.ID.equals(ID)&&this.name.equals(name);
    }

    @Override
    public int hashCode() {
        return name.hashCode()+ID.hashCode();
    }
}
class Systerm{
    private List<Product>products;
    public Systerm(){
        this.products=new ArrayList<>();
    }
    public void addProduct(Product product){
        products.add(product);
        System.out.println("商品添加成功！");
    }
    public boolean removeProduct(String ID,String name){
        Product target=new Product(ID,name);
        return products.remove(target);
    }

    public List<Product> findProduct(String ID) {
        List<Product> result=new ArrayList<>();
        for (Product product:products){
            if (product.getID().equals(ID)){
                result.add(product);
            }
        }
        return result;
    }
    public void displayProducts(){
        System.out.println("超市里所有的商品：");
        for (Product product:products){
            System.out.println(product);
        }
    }
}